#!/bin/sh
#
# SPDX-License-Identifier: Apache-2.0
# This file is part of termux-container.
#
# Copyright (c) 2023 Moe-hacker
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This file is used to init unshare containers.
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
# Mount system runtime directories.
mount -t proc proc -o rw,nosuid,nodev,noexec,relatime /proc
mount -t sysfs sysfs -o ro,seclabel,nosuid,nodev,noexec,relatime /sys
mount -t tmpfs tmpfs -o rw,seclabel,nosuid,size=65536k,mode=755 /dev
# The device list and permissions are from common docker containers.
mknod -m 622 /dev/console c 5 1
chown root:tty /dev/console
rm /dev/null
mknod -m 666 /dev/null c 1 3
mknod -m 666 /dev/zero c 1 5
mknod -m 666 /dev/ptmx c 5 2
chown root:tty /dev/ptmx
mknod -m 666 /dev/tty c 5 0
chown root:tty /dev/tty
mknod -m 444 /dev/random c 1 8
mknod -m 444 /dev/urandom c 1 9
ln -s /proc/self/fd /dev/fd
ln -s /proc/self/fd/0 /dev/stdin
ln -s /proc/self/fd/1 /dev/stdout
ln -s /proc/self/fd/2 /dev/stderr
ln -s /dev/null /dev/tty0
mkdir /dev/pts
mount -t devpts -o gid=4,mode=620 devpts /dev/pts
mkdir /dev/shm
mount -o rw,nosuid,nodev,mode=1777 -t tmpfs tmpfs /dev/shm
mkdir /dev/mqueue
mount -t mqueue none /dev/mqueue
mkdir /dev/net
mknod /dev/net/tun c 10 200
mount -o bind,private,ro /proc/sysrq-trigger /proc/sysrq-trigger
mount -o bind,ro,relatime /proc/bus /proc/bus
mount -o bind,ro,relatime /proc/fs /proc/fs
mount -o bind,ro,relatime /proc/irq /proc/irq
mount -o bind,ro,relatime /proc/sys /proc/sys
mount -o bind,ro,seclabel,relatime /proc/asound /proc/asound
mount -o bind,ro,seclabel,relatime /proc/scsi /proc/scsi
mount -o bind,ro,seclabel,relatime /sys/firmware /sys/firmware
if [ $(hostname) != $(cat /etc/hostname) ]; then
  hostname $(cat /etc/hostname)
fi
rm /dev/null && mknod -m 666 /dev/null c 1 3
printf "\033[0m"
/bin/su - root
